package com.wkk.yuli.controller;

import com.wkk.vo.Result;
import com.wkk.yuli.api.YuTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author : WangKeKe
 * @date : 2020/9/24 14:01
 * Description : 测试缓存机制
 */


@Slf4j
@RequestMapping("/yuLi/test")
@Api(tags = "RESTFUL-管理员-本地缓存")
@RestController
public class YuLiController {


    @Autowired
    private YuTemplate template;


    @ApiOperation("添加-有效时间")
    @PostMapping("add/{key}/{value}/{seconds}")
    public Result<String> add(@PathVariable("key") String key,
                              @PathVariable("value") Object value,
                              @PathVariable("seconds") Integer seconds) {
        template.setObject(key, value, seconds);
        return Result.ok("添加成功");
    }

    @ApiOperation("读取-单个读取")
    @GetMapping("/read/{key}")
    public Result<Object> read(@PathVariable("key") String key) {
        Object object = template.getObject(key);
        return object != null ? Result.ok(object) : Result.error(123, "不存在");
    }

    @ApiOperation("移除-单个移除")
    @DeleteMapping("/remove/{key}")
    public Result<String> remove(@PathVariable("key") String key) {
        template.removeObject(key);
        return Result.ok("移除成功");
    }

    @ApiOperation("数量-包括已失效")
    @GetMapping("/sum")
    public Result<Object> sum() {
        return Result.ok(template.getSize());
    }

    @ApiOperation("移除-所有移除")
    @DeleteMapping("/removeAll")
    public Result<String> removeAll() {
        template.clearAll();
        return Result.ok("移除成功");
    }

    @ApiOperation("状态-是否存在")
    @GetMapping("/has/{key}")
    public Result<Object> hasObject(@PathVariable("key") String key) {
        return template.hasObject(key) ? Result.ok("存在") : Result.error(123, "不存在");
    }

    @ApiOperation("读取-获取所有")
    @GetMapping("/all")
    public Result<Map<Object, Object>> all() {
        return Result.ok(template.getAllKeyValue());
    }
}
